﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using Triunfadores.BusinessEntities;
using Triunfadores.Repository.Service;

namespace Triunfadores.Repository
{
    public class BeneficiarioRepository:MasterRepository,IBeneficiarioRepository
    {
        public void AddBeneficiario(Beneficiario beneficiario)
        {
            Context.Beneficiarios.Add(beneficiario);
            Context.SaveChanges();
        }

        public Beneficiario FindSearchingFromBeneficiarioByDni(string dni)
        {
            var query = from p in Context.Beneficiarios
                        where p.Dni.Equals(dni)
                        select p;

            return query.SingleOrDefault();
        }

        public IEnumerable<Beneficiario> ObtenerTodosLosBeneficiarios()
        {
            var query = from p in Context.Beneficiarios
                        select p;

            return query.ToList();
        }


        public IEnumerable<Beneficiario> GetBeneficiarioByDni(string dniBeneficiario)
        {
            var query = (from b in Context.Beneficiarios
                         select b).Take(100);

            //filtro por nombre
            if (!string.IsNullOrEmpty(dniBeneficiario))
            {
                //query = (from c in query
                //         where c.Nombres.ToUpper().Contains(dniBeneficiario.ToUpper())
                //         select c).Take(100);
                
                query = from p in query
                        where p.Dni.Equals(dniBeneficiario)
                        select p;
            }

            return query.ToList();
        }


        public Beneficiario GetBeneficiarioById(int idBeneficiario)
        {
            return Context.Beneficiarios.Find(idBeneficiario);
        }


        public void UpdateBeneficiario(Beneficiario beneficiario)
        {
            Context.Entry(beneficiario).State=EntityState.Modified;
            Context.SaveChanges();
        }

        public void DeleteBeneficiario(int idBeneficiario)
        {
            throw new NotImplementedException();
        }
    }
}
